﻿using AsmShaderConverter.AsmShader;
using AsmShaderConverter.HlslShader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsmShaderConverter.AsmFunctions
{
    public class AsmFunctionMul : IConvertToHlsl
    {
        readonly string originalLineOfCode;
        private Register destination;
        private Register source1;
        private Register source2;
        public Register GetDestinationRegister()
        {
            return this.destination;
        }

        public AsmFunctionMul(string lineOfCode)
        {

            this.originalLineOfCode = lineOfCode;

            lineOfCode = lineOfCode.Replace(",", ""); // Remove commas.
            lineOfCode = lineOfCode.Trim(); // Remove dead space.
            var values = lineOfCode.Split(' '); // split

            switch (values.Length)
            {
                case 4:
                    this.destination = new Register(values[1], Swizzle.StandardXYZW);
                    this.source1 = new Register(values[2], destination.swizzle);
                    this.source2 = new Register(values[3], destination.swizzle);
                    break;
                default:
                    throw new Exception("Function can´t be converted! " + lineOfCode);
            }

        }

        public string ToHLSL(AsmShaderFile asmShader)
        {
            Swizzle.SetSwizzleLengthToDestination(destination, source1);
            Swizzle.SetSwizzleLengthToDestination(destination, source2);

            //if (destination.swizzle.Length == 0)
            //    destination.swizzle = new char[] { 'x', 'y', 'z', 'w' };

            //if (source1.swizzle.Length == 0)
            //    source1.swizzle = destination.swizzle;
            //if (source2.swizzle.Length == 0)
            //    source2.swizzle = destination.swizzle;

            return String.Format("{0} = {1} * {2};", this.destination.ToHLSL(asmShader), this.source1.ToHLSL(asmShader), this.source2.ToHLSL(asmShader));
        }


        public string ToHLSLWitchComment(AsmShaderFile asmShader)
        {
            return this.ToHLSL(asmShader) + "//" + this.originalLineOfCode;
        }
    }
}
